package com.rybbaby.tss.core.utils;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.CharSetUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 谢进伟
 * @description 字符串操作工具(扩展自org.apache.commons.lang.StringUtils)
 * @createDate 2016-2-19 下午00:36:43
 */
public class StringUtils extends org.apache.commons.lang.StringUtils {

    /**
     * 将字符串根据java变量规则(驼峰式)进行格式化
     *
     * @param str 需要转换的字符串
     * @return
     */
    public static String converToJavaField(String str) {
        return StringUtils.converToJavaVariable(str, true);
    }

    /**
     * 将字符串根据java变量规则(驼峰式)进行格式化
     *
     * @param str              需要转换的字符串
     * @param firstChatIsLower 首字母是否小写
     * @return
     */
    public static String converToJavaVariable(String str, boolean firstChatIsLower) {
        List<Character> list = new ArrayList<Character>();
        char[] numberChars = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char c = str.charAt(i);
            if (i == 0) {
                if (c == '_') {
                    if (firstChatIsLower) {
                        list.add(Character.toLowerCase(str.charAt(++i)));
                    } else {
                        list.add(Character.toUpperCase(str.charAt(++i)));
                    }
                } else if (ArrayUtils.contains(numberChars, c)) {
                    list.add('_');
                    if (firstChatIsLower) {
                        list.add(Character.toLowerCase(c));
                    } else {
                        list.add(Character.toUpperCase(c));
                    }
                } else {
                    if (firstChatIsLower) {
                        list.add(Character.toLowerCase(c));
                    } else {
                        list.add(Character.toUpperCase(c));
                    }
                }
            } else if (c != '_') {
                if (c == '-' || c == ',' || c == '、' || c == '，' || c == '/' || c == '|') {
                    list.add('_');
                } else {
                    list.add(c);
                }
            } else {
                if (i != length - 1) {
                    Character c1 = str.charAt(++i);
                    list.add(Character.toUpperCase(c1));
                }
            }
        }
        String newStr = StringUtils.join(list, "");
        return newStr;
    }

    /**
     * 根据javaBean标准获取一个属性对应的getter/setter的后缀
     *
     * @param fileName 属性名称
     */
    public static String getGetterOrSetterSuffix(String fileName) {
        if (isBlank(fileName)) {
            return null;
        }
        if (fileName.length() == 1) {
            return fileName.toUpperCase();
        } else {
            char firstChart = fileName.charAt(0);
            char secondChart = fileName.charAt(1);
            if ((secondChart > 'A' && secondChart < 'Z') || (firstChart > 'A' && firstChart < 'Z')) {
                return fileName;
            } else {
                return converToJavaVariable(fileName, false);
            }
        }
    }

    /**
     * 判断Object类型
     *
     * @param obj
     * @return
     */
    public static boolean isBlank(Object obj) {
        if (obj == null) {
            return true;
        } else {
            return isBlank(obj.toString());
        }
    }

    /**
     * 判断Object类型
     *
     * @param obj
     * @return
     */
    public static boolean isNotBlank(Object obj) {
        return !isBlank(obj);
    }

    /**
     * 判断字符串是否为绝对不空
     *
     * @param obj 需要判断的字符串
     * @return
     */
    public static boolean isAbsoluteNotNull(Object obj) {
        return isNotBlank(obj) && !"null".equals(obj);
    }

    /**
     * 获取指定字符串的编码
     *
     * @param str 字符串
     * @return
     */
    public static String getEncoding(String str) {
        String encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                return encode;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                return encode;
            }
        } catch (Exception exception2) {
        }
        encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                return encode;
            }
        } catch (Exception exception) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                return encode;
            }
        } catch (Exception exception3) {
        }
        return null;
    }
}
